package com.example.dynamicdatabase.controller;

import com.example.dynamicdatabase.databasemodel.DataBaseInfo;
import com.example.dynamicdatabase.dynamicconfig.CommonUtils;
import com.example.dynamicdatabase.service.DataBaseInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description 动态切换数据源 查询数据
 * @author 奶茶
 * Created by on 2018/11/26 9:24.
 */
@RestController
public class DynamicSetDataSourceController {

    @Autowired
    private DataBaseInfoService dataBaseInfoService;

    @RequestMapping(value = "/dynamicDataSource",method = RequestMethod.POST)
    public @ResponseBody Object dynamicDataSource(HttpServletRequest request, String appKey){
        DataBaseInfo dataBaseInfo = dataBaseInfoService.getDataBaseInfoByAppKey(appKey);
        if(dataBaseInfo != null){
            CommonUtils.dynamicSwitchDataBase(dataBaseInfo);
            if(dataBaseInfo.getAppKey().equals("OCES")){
                return dataBaseInfoService.getDynamicData();
            }else {
                return dataBaseInfoService.getDynamicData2();
            }

        }
        return null;
    }

}
